package com.example.utils;

import com.example.common.Constants;
import com.example.config.ClientConfig;
import com.example.interceptor.SSOSessionData;
import com.example.interceptor.UserSessionHolder;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.util.Properties;

/**
 * @desc 读取配置文件
 * @author shishumin
 * @date 2024/9/4 20:59
 */
public class PropertyUtil {

    /**
     * @desc 加载配置文件
     * @author shishumin
     * @date 2024/9/5 08:41
     */
    public static String loadProperties(String name) {
        Properties prop = new Properties();
        InputStream inputStream = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");

        if (inputStream != null) {
            try {
                prop.load(inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return prop.getProperty(name);
    }

    /**
     * @desc 加载配置文件
     * @author shishumin
     * @date 2024/9/5 08:41
     */
    public static String getUserName() {

        //获取用户名
        SSOSessionData currentUser = UserSessionHolder.getCurrentUser();
        if (currentUser != null) {
            String userName = currentUser.getUsername();
            String keyfile = userName + ".p12";
            System.out.println("keyfile:" + keyfile);
            return keyfile;
        }

        String username = ClientConfig.account_keyfile;
        // 配置文件自定义
        if (StringUtils.isNotBlank(username)) {
            return username;
        }
        username = loadProperties(Constants.TOKEN_USER_KEY);
        System.out.println("username:" + username);
        return username;
    }

    /**
     * @desc 加载配置文件
     * @author shishumin
     * @date 2024/9/5 08:41
     */
    public static String getRealName() {

        //获取用户名
        SSOSessionData currentUser = UserSessionHolder.getCurrentUser();
        if (currentUser != null) {
            return currentUser.getRealm();
        }

        // 配置文件自定义
        String realmId = loadProperties(Constants.TOKEN_REAL_KEY);
        return realmId;
    }

}
